/*
 *  levelUpGame - SerialDevice.cpp
 *  Copyright 2011 Arne Boon. All rights reserved.
 */
#include "ofMain.h"
#include "SerialDevice.h"
#include <string>

//--------------------------------------------------------------
SerialDevice::SerialDevice()
{
	ofLog( OF_LOG_NOTICE, "---SerialDevice constructor---" );

	bytesRequired = NUM_BYTES;
	bytesRemaining = NUM_BYTES;

	countCycles = 0;
	waitingCycles = 80;
}

//--------------------------------------------------------------
SerialDevice::~SerialDevice()
{
	//
}

//--------------------------------------------------------------
void SerialDevice::setup(int _deviceId, int _baud)
{
	//serial.enumerateDevices();
	ofLog( OF_LOG_NOTICE, "---SERIAL DEVICE: connecting to deviceId: %i at baudRate: %i", _deviceId, _baud);
	serial.setup(_deviceId, _baud);
}


//--------------------------------------------------------------
void SerialDevice::update()
{
	if ( bytesRemaining > 0)
	{
		// check for data
		if ( serial.available() > 0 )
		{
			//reset measurements cycles
			countCycles = 0;

			// try to read - note offset into the bytes[] array, this to not overwrite bytes we already have
			int bytesArrayOffset = bytesRequired - bytesRemaining;
			int result = serial.readBytes( &bytesReturned[bytesArrayOffset], bytesRemaining );
			
			//printf("RAW DATA result = %i /n", result);

			// check for error code
			if ( result == OF_SERIAL_ERROR )
			{
				// something bad happened
				ofLog( OF_LOG_ERROR, "unrecoverable error reading from serial" );
			}
			else if ( result == OF_SERIAL_NO_DATA )
			{
				// nothing was read, try again
			}

			else
			{
				// we read some data!
				bytesRemaining -= result;
			}
		} else {
			//if no serial data available, wait some cycles to give the serial port some time to recover
			countCycles++;
			if(countCycles > waitingCycles) {
				//cout << "...waiting" << endl;
			}
		}
	}

	//--------------------------------------
	else {

		/* get incoming bytes (8) and parse them, splitting on character '.'
		 * j0.p0.s0
		 * j0.p0.s1
		 * 01234567 = 8 bytes
		 * then do check if letters j, p, s are there (maybe)
		 * filter int, int, bool out of bytes
		 * set values to argsJoystick arguments
		 * call ofNotifyEvent
		 */

		if (bytesReturned[0] == '-') {
			ofLog( OF_LOG_NOTICE, "---SerialDevice: HELLO FROM ARDUINO---" );
		} else {
			ofLog( OF_LOG_NOTICE, "---SerialDevice: JOYSTICK MESSAGE -------" );
			
			if (bytesReturned[0] = 'j') {
				argsJoystick.joystickNum = bytesReturned[1] - '0'; //parse int
				ofLog( OF_LOG_NOTICE, "---SerialDevice: joystick num = %i", argsJoystick.joystickNum);
			}
			
			if (bytesReturned[3] = 'p') {
				argsJoystick.joystickPinNum = bytesReturned[4] - '0'; //parse int
				argsJoystick.joystickDirection = (EventArgsJoystick::DIRECTION)argsJoystick.joystickPinNum;
				ofLog( OF_LOG_NOTICE, "---SerialDevice: pin num = %i", argsJoystick.joystickPinNum);
			}
			
			if (bytesReturned[6] = 's') {
				argsJoystick.joystickStatus = ( bytesReturned[7] == '1' ? true : false ); //parse bool
				ofLog( OF_LOG_NOTICE, "---SerialDevice: status = %d", argsJoystick.joystickStatus);
			}
			
			ofNotifyEvent(onJoystickMoved, argsJoystick, this);
		}

		//reset serial for next reading
		serial.flush();
		bytesRemaining = NUM_BYTES;
	}

}

//--------------------------------------------------------------
void SerialDevice::close() {
	
	serial.flush();
	serial.close();
	//serial.enumerateDevices();
	
}
